package com.overlord.im.commons.message;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * @ClassName Byte2MessageDecoder
 * @Author wuwei
 * @Description NONE
 * @Date 2020/5/29 17:51
 **/
public class MessageDecoder extends LengthFieldBasedFrameDecoder {

    private Protocol protocol;

    public MessageDecoder(Protocol protocol) {
        super(ProtocolConstant.DATA_MAX_SIZE, 13, 3);
        this.protocol = protocol;
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        ByteBuf frame = (ByteBuf)super.decode(ctx, in);
        OverlordImMessage decode = protocol.decode(frame);
        ctx.fireChannelRead(decode);
        return null;
    }
}
